package com.xliang.spider.version04.service.impl;

import com.xliang.spider.util.ConnectionUtil;
import com.xliang.spider.version04.dao.SpiderLessonDao;
import com.xliang.spider.version04.pojo.LessonEntity;
import com.xliang.spider.version04.service.SpiderLessonService;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * ${DESCRIPTION}
 *
 * @author xiongl
 * @create 2016-06-21 13:28
 */
@Service
public class SpiderLessonServiceImpl implements SpiderLessonService
{
    
    private static Logger logger = Logger.getLogger(SpiderLessonServiceImpl.class);
    
    @Resource
    private SpiderLessonDao spiderScanLessonDao;
    
    @Override
    public void scanLesson()
        throws Exception
    {
        Document document = ConnectionUtil.getConnection("http://www.jikexueyuan.com/").get();
        Elements lessons = document.getElementsByClass("lesson-list-show").select(".cf").select("a");
        for (Element lesson : lessons)
        {
            int lessonId = Integer.parseInt(lesson.attr("cgid").trim());
            String lessonName = lesson.text().trim().replaceAll("\\\\|:|\\*|\\?|\"|<|>|\\||\u000B|/n|/r|/t", "").replaceAll("/", "&");
            String lessonUrl = lesson.attr("href").trim();
            LessonEntity le = new LessonEntity(lessonId, lessonName, lessonUrl);
            try
            {
                spiderScanLessonDao.insertLesson(le);
                logger.info("新增课程：" + le.getLessonName());
            }
            catch (Exception e)
            {
                if (e.getMessage().contains("Duplicate") && e.getMessage().contains("uniq_lesson_id_and_lesson_name"))
                {
                    spiderScanLessonDao.updateLesson(le);
                    logger.info("更新课程：" + le.getLessonName());
                }
                else
                {
                    logger.error("扫描视频：" + le.getLessonName() + "出现异常");
                    throw new Exception(e);
                }
            }
        }
    }
    
    public static void main(String[] args)
        throws Exception
    {
        SpiderLessonServiceImpl s = new SpiderLessonServiceImpl();
        s.scanLesson();
    }
}
